package com.example.jf.jfapp;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.widget.TextView;

import com.example.jf.jfapp.adapter.XuexiAdapter;
import com.example.jf.jfapp.bean.request.BlogRequest;
import com.example.jf.jfapp.bean.response.BlogResponse;
import com.example.jf.jfapp.utils.AppConfigUtils;
import com.example.jf.jfapp.utils.Constant;
import com.google.gson.Gson;

import java.io.IOException;
import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

import static com.example.jf.jfapp.utils.OkHttpUtils.getOkHttpClient;
import static com.example.jf.jfapp.utils.RequestUtils.requestPOST;

public class DetailActivity extends AppCompatActivity {
    public static final String DETAILTYPE = "TYPE";
    private static final String TAG = "DetailActivity";
    @BindView(R.id.detailtitle)
    TextView detailtitle;
    @BindView(R.id.xuexiRv)
    RecyclerView xuexiRv;

    ArrayList<BlogResponse.ResultBean> mResultBean = new ArrayList<>();

    private BlogRequest mBlogRequest;
    private XuexiAdapter mXuexiAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_detail);

        ButterKnife.bind(this);

        int type = getIntent().getIntExtra(DETAILTYPE, -1);

        if(type==1){
            detailtitle.setText("杂谈");
        }
        if(type==2){
            detailtitle.setText("帮助");
        }
        if(type==3){
            detailtitle.setText("分享");
        }
        xuexiRv.setLayoutManager(new LinearLayoutManager(this));

        mXuexiAdapter = new XuexiAdapter(mResultBean);

        xuexiRv.setAdapter(mXuexiAdapter);


        mBlogRequest = new BlogRequest(type, 10, 0);
        //获取用户信息
        mBlogRequest.setUserId(AppConfigUtils.getInstance().getLoginInfo(this).getObjectId());

        getXuexiData();


    }
    private void getXuexiData() {
        final Request request = requestPOST(mBlogRequest, Constant.URL.BLOG_LIST);
        OkHttpClient okHttpClient = getOkHttpClient();
        okHttpClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Log.e(TAG, "获取博客列表失败 ");
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                if (response.code() == 200) {
                    String result = response.body().string();
                    Log.e(TAG, "获取博客列表成功: " + result);
                    if (result != null) {
                        BlogResponse blogResponse = new Gson().fromJson(result, BlogResponse.class);
                        mResultBean.addAll(blogResponse.getResult());
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                mXuexiAdapter.notifyDataSetChanged();
                            }
                        });

                    }
                }
            }
        });


    }
}
